﻿using KinonekoSoftware.CodariaDB.Presets;

namespace KinonekoSoftware.CodariaDB.Core
{
    public sealed class IconSystem : _File
    {
        public IconSystem() : base("Icons", ResourceType.Icon)
        {

        }
        
        
        protected override Occupation OnCreate(string id) => new Occupation
        {
            Id       = id,
            FileName = id,
            Usage    = true,
            Type     = ResourceType.Icon,
        };

        #region GetPresetIcon

        
        public static byte[] GetPresetIcon(Preset_Icon icon)
        {
            if (icon == Preset_Icon.Override)
                icon = Preset_Icon.Icon_1;

            var index    = (int)icon;
            var name     = $"{PresetManager.FileName_Prefix}{PresetManager.FileName_Icon[index]}";
            var assembly = typeof(PresetManager).Assembly;

            var r = PresetManager.ExtractResourceToBuffer(assembly, name);

            if (!r.IsFinished)
            {
                return Array.Empty<byte>();
            }

            return r.Value;
        }

        #endregion
        
        #region GetPresetIconFilePath

        
        public static string GetPresetIconFilePath(string workspace)
        {
            var presets = GetDir(null, Path.Combine(workspace, "Presets"));
            return $@"{presets}\icon.png";
        }

        public string GetPresetIconFilePath()
        {
            var presets = GetDir(null, Path.Combine(Workspace, "Presets"));
            return $@"{presets}\icon.png";
        }

        #endregion

        #region OverrideIcon

        
        public async Task OverrideIcon(byte[] buffer)
        {
            await File.WriteAllBytesAsync(GetPresetIconFilePath(), buffer);
        }

        public static async Task OverrideIcon(string workspace, byte[] buffer)
        {
            await File.WriteAllBytesAsync(GetPresetIconFilePath(workspace), buffer);
        }

        #endregion

        public override string EngineID => EngineNames.FileDataSet_Icon;
    }
}